<template>
    <div class="manage">
        <!-- 弹出的对话框 -->
        <el-dialog
            :title="operateType === 'add' ? '新建用户' : '编辑用户'"
            :visible.sync = "isShow"
        >
            <common-form
                :formLabel="operateFormLabel"
                :form="operateForm"
                :inline="true"
                ref="form"
            > </common-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="isShow = false; getList()">取消</el-button>
                <el-button type="primary" @click="confirm">确定</el-button>
            </div>
        </el-dialog>
        <div class="manage-header">
            <el-button type="primary" @click="addUser">+ 新增用户</el-button>
            <common-form
                :formLabel="formLabel"
                :form="searchFrom"
                :inline="true"
                ref="form"
            >
                <el-button type="primary" @click="getList(searchFrom.keyword)">搜索</el-button>
            </common-form>
        </div>
        <common-table
            :tableData="tableData"
            :tableLabel="tableLabel"
            :config="config"
            @changePage="getList()"
            @edit="editUser"
            @del="delUser"
        ></common-table>
    </div>
</template>

<script>

import CommonForm from '../../src/components/CommonForm.vue'
import CommonTable from '../../src/components/CommonTable.vue'
import { getUser } from '../../api/data'

export default {
    name: 'user',

    components: {
        CommonForm,
        CommonTable
    },

    data () {
        return {
            // 当前操作状态是新增还是编辑
            operateType: 'add',
            // 对话框是否显示
            isShow: false,

            // 编辑框
            operateFormLabel: [
                    {
                        model: 'name',
                        label: '用户名',
                        type: 'input'
                    },
                    {
                        model: 'password',
                        label: '密码',
                        type: 'password',
                        content: '密码需包含大小写字母、数字,最少8个字符,最多30个字符'
                    },

                    {
                        model: 'comfirmPassword',
                        label: '确认密码',
                        type: 'password',
                        content: '请再次输入密码'
                    },

                    {
                        model: 'authority',
                        label: '权限',
                        type: 'select',
                        opts: [
                            {
                                label: '管理员',
                                value: 1
                            },
                            {
                                label: '普通用户',
                                value: 0
                            }
                        ]
                    }
            ],
            operateForm: {
                name: '',
                password: '',
                comfirmPassword:  '',
                authority: ''
            },

            // 搜索框
            formLabel: [
                {
                    model: "keyword",
                    label: '',
                    type: 'input'
                }
            ],
            searchFrom:{
                keyword: ''
            },

            tableData: [],
            tableLabel: [
                {
                    prop:  "name",
                    label: '用户名'
                },
                {
                    prop:  "authority",
                    label: '权限'
                }
            ],
            config: {
                page: 1,
                total: 30
            }
        }
    },
    methods: {
        confirm(){
            if(this.operateType === 'edit') {
                this.$http.post('/user/edit',this.operateForm).then(res => {
                    console.log(res)
                    this.isShow = false
                    this.getList()

                })
            }
            else{
                this.$http.post('/user/add',this.operateForm).then(res =>{
                    console.log(res)
                    this.isShow = false
                    this.getList()
                })
            }
        },
        addUser(){
            this.isShow = true
            this.operateType = 'add'
            this.operateForm = {
                name: '',
                password: '',
                comfirmPassword:  '',
                authority: ''
            }
        },

        getList(name = '') {
            this.config.loading = true
            name ? (this.config.page = 1) : ''
            getUser({
                page: this.config.page,
                name
            }).then(({ data : res }) => {
                this.tableData = res.list.map(item => {
                    item.authority = (item.authority === 1 ? "管理员" : "普通用户")
                    return item
                })
                this.config.total = res.count
                this.config.loading = false
            })

        },

        editUser(row) {
            this.operateType = 'edit'
            this.isShow = true
            this.operateForm = row
        },

        delUser(row) {
            this.$confirm("此操作将永久删除此组件,是否继续？","提示",{
                confirmButtonText: "确认",
                cancelButtonText: "取消",
                type: "warning"
            }).then(() => {
                const id = row.id
                this.$http.get("/user/del",{
                    params: { id }
                }).then(()=>{
                    this.$message({
                        type: 'success',
                        message: '删除成功'
                    })
                    this.getList()
                })
            })
        }
    },
    created(){
        this.getList()
    }
}

</script>


<style lang="less" scoped>
    .manage-header{
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 20px;
    }
</style>